/*
 * stnLexicalJson.h
 *
 *  Created on: Jul 12, 2013
 *      Author: alexandru
 */

#include "stnJson.h";
#include <typeinfo>
#include <fstream>

using namespace std;


#ifndef STNLEXICALJSON_H_
#define STNLEXICALJSON_H_


inline bool IsWhitespace(char c)
	{
		return (c == '\n' || c == ' ' || c == '\t' || c == '\r' || c == '\f');
	}

inline bool IsNumber(char c)
{
	return ((c >= '0' && c <= '9') || c == '.' || c == '-');
}



//void convertFromString(string command);


void getFromFile(){
	string text;

//	FILE *a;
//	 a=fopen("sample.json","r");
//
//	 while(fgetc(a)!=EOF){
//		 cout << fgetc(a) << endl;
//	 }
//
//	 fclose(a);

//
	string res;
	string finalRes;
	char *path="sample.json";
		ifstream fin;                          // Declaring an input stream object
		string outPut;
		string result="";
		fin.open(path, ios::in);
		if(fin.is_open())                      // If it opened successfully
			{
				while (getline(fin, outPut)) {
					//cout << outPut << endl;
					res.append(outPut);
					res.append("\n");
				}
				fin.close();
				//convertFromString(res);

					for (unsigned i=0; i<res.length(); ++i){
						if(!IsWhitespace(res.at(i))){
							finalRes+=res.at(i);
						}
					}
			}
		else{
			printf("FAILED TO LOAD FILE %s", path);
		}
}





void convertFromString(string str, Object obj){
	//cout << str << endl;
	unsigned b_first = str.find("{");

	unsigned b_last = str.find_last_of("}");
	string res;
	string rest;

	//daca e root
	if(b_first==0 && b_last==str.length()-1){
		cout << b_first << " -- " << b_last << " -- " << str.length()-1 << endl;
		res=str.substr(b_first+1, b_last-1);
		cout << "verification passed, continue..." << endl;
		convertFromString(res, obj);
		return;
	}


cout << str << endl;

bool numC=0;
string propName;
vector<string> props;

string propVal;
vector<string> vals;

int scan=0;

for (unsigned i=0; i<str.length(); ++i){
			if(!IsWhitespace(str.at(i))){
				char c=str.at(i);

				if(c=='\"'){
					scan=0;
				}

				if(c==':'){
						scan=1;
					}


				if(c=='{'){
					scan=2;
				}


				if(scan==0){
					propName+=c;
					props.push_back(propName);
				}

				if(scan==1){
					propVal+=c;
				}

			}
		}



for (int i =0; i<props.size(); i++){
	cout << "propName: " << props.at(i) << endl;
}







	cout << res << endl;


}











void append(char* s, char c)
{
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
}


class jsc{



private :

public :
	jsc(){

	};
	~jsc(){

	};

	Object getJsonObject(string input, Object object){
		bool isDin=false;
		bool isNum=false;
	//
	//string str=new string();
		string restToScan;


	//const char * item=input.c_str();
	//
	string str;
//	string str2;
	cout << "scan in " << object.getName() << "  input: " << input<< endl;

//	unsigned inputLength=input.length();

	for (unsigned i=0; i<input.length(); ++i)
				  {
					char c=input.at(i);
					//std::cout << c << endl;


					if(c=='.'){
						break;
					}



					if(c=='['){
						isDin=true;
						break;
					}

					if(c==']'){
						isDin=true;
						break;
					}

					if(c!='['){
						str+=input.at(i);
					}



				  }


	cout << "str.length()"<< str.length() << " " << str<< endl;
	cout << "input.length()" << input.length()<< " "<< input << endl;


	if(!isDin){
		cout << "object is not dinamic" << endl;
		if(str.length()!=input.length() && str.length()>0){// daca are restToGet
			restToScan=input.substr(str.length()+1, input.length());
			cout << " ... continue recursive with name "<< str << " restToScan= " << restToScan << " length:" << restToScan.length() << endl;
			Object obj=object.getByName(str);
			cout << " continue recursive in object " << obj.getName()  << endl;
			return getJsonObject(restToScan,obj );
		}
	}

	else{
		cout << "object IS dinamic" << endl;

		if(str.length()>0){
			cout << "object has name"<< str << "  "<< str.length() << endl;
			restToScan=input.substr(str.length(), input.length()-1);
			Object obj=object.getByName(str);
			cout << " continue recursive in object " << obj.getName()  << endl;
			return getJsonObject(restToScan, obj);
		}else{
			cout << "object has no name"<< str << "  "<< str.length() << endl;
			cout << "retrun to input: "<< input << endl;
			string::size_type loc1 = input.find('[', 0 );
			string::size_type loc2 = input.find(']', 0 );
			str=input.substr(loc1+1, loc2-1);
			cout << "between brackets:  "<< str << endl;
			restToScan=input.substr(loc2+1, input.length()-1);

			if(restToScan.length()>0){
				cout << " ... continue recursive with name "<< str << " restToScan= " << restToScan << " length:" << restToScan.length() << endl;
				Object obj=object.getByIndex(str);
				return getJsonObject(restToScan, obj);
			}else{
				return object.getByIndex(str);
			}

		}

	}



		cout << "final case:  getByName(" << str << ");"<< endl;
		return object.getByName(str);


	}


};




#endif /* STNLEXICALJSON_H_ */
